/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.portalcenter.dto.UserDTO;
import cn.hallele.mc.sdk.portalcenter.dto.UserQueryDTO;
import cn.hallele.mc.sdk.usercenter.common.enums.SexEnum;
import cn.hallele.mc.sdk.usercenter.common.enums.UserStatusEnum;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.UserBO;
import cn.hallele.ms.portalcenter.application.bo.UserQueryBO;
import cn.hallele.ms.starter.web.framework.ActionExecutor;
import cn.hallele.ms.starter.web.framework.context.ActionContext;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * hallele_ms_usercenter
 *
 * @author anle5
 * @since 2024/10/5 16:15
 */
@Slf4j
@Service
public class UserDBDas extends ModelDas<ActionExecutor, UserDTO, UserBO> {
    protected UserDBDas(ActionExecutor option) {
        super(option);
    }

    private static final String GET_LOGIN_USER = "queryLoginUserInfoApplicationService";
    private static final String QUERY_USER_LIST = "queryUserListApplicationService";

    /**
     * 获取当前登录用户信息
     *
     * @return UserDTO
     */
    public UserDTO me() {
        ActionContext context = new ActionContext();
        this.option.execute2Resp(GET_LOGIN_USER, null, context);
        UserBO result = context.getResult();
        return revert(result);
    }

    /**
     * 用户分页查询
     *
     * @param queryParams queryParams
     * @return UserDTO
     */
    public Page<UserDTO> page(@Valid UserQueryDTO queryParams) {
        ActionContext context = new ActionContext();
        this.option.execute2Resp(QUERY_USER_LIST, convertQuery(queryParams), context);
        Page<UserBO> result = context.getResult();
        return revertPage(result);
    }

    /**
     * 转换UserQueryDTO -> UserQueryBO
     *
     * @param userQueryDTO UserQueryDTO
     * @return UserQueryBO
     */
    private UserQueryBO convertQuery(UserQueryDTO userQueryDTO) {
        return ObjectUtil.map(userQueryDTO, u -> UserQueryBO.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .deptId(u.getDeptId())
                .username(u.getUsername())
                .phoneNumber(u.getPhoneNumber())
                .status(ObjectUtil.map(u.getStatus(), UserStatusEnum::valueOfCode))
                .keywords(u.getKeywords())
                .pageNo(u.getPageNo())
                .pageSize(u.getPageSize())
                .startTime(u.getStartTime())
                .endTime(u.getEndTime())
                .build());
    }

    /**
     * 将T转成E
     *
     * @param userDTO 被转换者
     * @return S 转换后者
     */
    @Override
    protected UserBO convert(UserDTO userDTO) {
        return ObjectUtil.map(userDTO, u -> UserBO.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .username(u.getUsername())
                .avatar(u.getAvatar())
                .phoneNumber(u.getPhoneNumber())
                .email(u.getEmail())
                .sex(SexEnum.valueOfCode(u.getSex()))
                .age(u.getAge())
                .status(ObjectUtil.map(u.getStatus(), UserStatusEnum::valueOfCode))
                .personalitySignature(u.getPersonalitySignature())
                .passExpiredTime(u.getPassExpiredTime())
                .accountExpiredTime(u.getAccountExpiredTime())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .createBy(u.getCreateBy())
                .updateBy(u.getUpdateBy())
                .perms(u.getPerms())
                .roleCodes(u.getRoles())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param userBO 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected UserDTO revert(UserBO userBO) {
        return ObjectUtil.map(userBO, u -> UserDTO.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .username(u.getUsername())
                .avatar(u.getAvatar())
                .phoneNumber(u.getPhoneNumber())
                .email(u.getEmail())
                .sex(ObjectUtil.map(u.getSex(), SexEnum::getCode))
                .age(u.getAge())
                .status(ObjectUtil.map(u.getStatus(), UserStatusEnum::getCode))
                .personalitySignature(u.getPersonalitySignature())
                .passExpiredTime(u.getPassExpiredTime())
                .accountExpiredTime(u.getAccountExpiredTime())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .createBy(u.getCreateBy())
                .updateBy(u.getUpdateBy())
                .perms(u.getPerms())
                .roles(u.getRoleCodes())
                .build());
    }

    /**
     * UserQueryDTO 转 UserBO
     *
     * @param userQueryDTO 查询条件
     * @return UserBO
     */
    private UserBO convert(UserQueryDTO userQueryDTO) {
        return ObjectUtil.map(userQueryDTO, u -> UserBO.builder()
                .status(ObjectUtil.map(u.getStatus(), UserStatusEnum::valueOfCode))
                .pageNo(u.getPageNo())
                .pageSize(u.getPageSize())
                .keywords(u.getKeywords())
                .startTime(u.getStartTime())
                .endTime(u.getEndTime())
                .build());
    }
}
